﻿@{
    ViewBag.Title = "SyncAccountingIndex";
    Layout = "~/Views/Shared/_Form.cshtml";

    var zyjzDjEditable = (ViewBag.zyjz_dj_editable as bool?) ?? false;
}

<div class="rows" style="margin-bottom: 1%;">
    <div class="panel panel-default" style="margin-bottom:0;">
        <div class="panel-heading navb-bg">
            住院病人信息
        </div>
        <table class="form" style="width:96%;">
            <tbody>
                <tr>
                    <th class="formTitle">住院号：</th>
                    <td class="formValue" colspan="2">
                        <input class="form-control form-control-focus" type="text" id="zyh" value="" style="width:49%;float:left;" />
                        &nbsp;&nbsp;<input type="button" class="btn btn-default btn-md btn-default-color" title="选择住院病人" style="width: 17%;" id="btnsyy" value="查询" onclick="GetPatSerarchView($('#zyh').val());">
                    </td>
                    <td class="formValue">
                        <input type="button" id="btn_search" class="btn btn-primary btn-md" value="&nbsp;&nbsp;查&nbsp;&nbsp;询&nbsp;&nbsp;">
                    </td>
                    <th class="formTitle">姓名：</th>
                    <td class="formValue">
                        <label id="xm"></label>
                    </td>
                    <th class="formTitle">性别：</th>
                    <td class="formValue">
                        <label id="xb"></label>
                    </td>
                    <th class="formTitle">年龄：</th>
                    <td class="formValue">
                        <label id="nlshow"></label>
                    </td>
                    <th class="formTitle">证件号：</th>
                    <td class="formValue">
                        <label id="zjh"></label>
                    </td>
                </tr>
            </tbody>
            <tbody class="dispTbody" style="display:none;">
                <tr>
                    <th class="formTitle">入院诊断：</th>
                    <td class="formValue" colspan="2">
                        <label id="ryzd"></label>
                    </td>
                    <th class="formTitle">手机号：</th>
                    <td class="formValue">
                        <label id="phone"></label>
                    </td>
                    <th class="formTitle">出生日期：</th>
                    <td class="formValue">
                        <label id="csny"></label>
                    </td>
                    <th class="formTitle">入院日期：</th>
                    <td class="formValue">
                        <label id="ryrq"></label>
                    </td>
                </tr>
            </tbody>
        </table>
        <div class="fa_icontoggle"><i class="fa fa-angle-double-down icontoggle" style="color:rgb(0, 160, 233);cursor:pointer;"></i></div>
    </div>
</div>
<div class="rows" style="margin-bottom: 1%;">
    <div class="panel panel-default" style="margin-bottom:0;">
        <div class="panel-heading navb-bg">
            记账项目
        </div>
        <div id="divSLMX" style="padding: 2px;padding-right:20px;">
            <table class="form">
                <tr>
                    <th class="formTitle"><span class="required">*</span>收费项目：</th>
                    <td class="formValue">
                        <input id="sfxmmc" type="text" class="form-control  form-an" />
                        <input id="sfxmCode" type="text" style="display:none;" class="form-control" />
                        <!-- 医嘱类型 1药品 2收费项目 -->
                        <input id="yzlx" type="text" style="display:none;" class="form-control" />

                        <input id="sfdlCode" type="text" style="display:none;" class="form-control" />
                        <input id="sfdlmc" type="text" style="display:none;" class="form-control" />
                    </td>
                    <th class="formTitle">单位时长：</th>
                    <td class="formValue">
                        <input id="duration" type="text" class="form-control  form-an" value="" />
                    </td>
                    <th class="formTitle"><span class="required">*</span>单位：</th>
                    <td class="formValue">
                        <label id="dw"></label>
                    </td>
                    <th class="formTitle"><span class="required">*</span>单价(元)：</th>
                    <td class="formValue">
                        @if (zyjzDjEditable)
                        {
                            <input id="dj" name="dj" type="text" class="form-control newtouch_Readonly" />
                        }
                        else
                        {
                            <label id="dj"></label>
                        }
                    </td>
                    <th class="formTitle"></th>
                    <td class="formValue" style="padding-top: 1px;">
                        <div class="ckbox">
                            <input id="ttbz" name="ttbz" type="checkbox"><label for="ttbz">团体治疗</label>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th class="formTitle"><span class="required">*</span>数量：</th>
                    <td class="formValue">
                        <input id="sl" type="text" class="form-control  form-an" />
                    </td>
                    <th class="formTitle"><span class="required">*</span>治疗师：</th>
                    <td class="formValue">
                        <input id="zlsList" readonly="readonly" type="text" class="form-control form-an" value="" />
                    </td>
                    <th class="formTitle"><span class="required">*</span>康复类别：</th>
                    <td class="formValue">
                        <select id="kflb" name="kflb" class="form-control">
                            <option value="">==请选择==</option>
                        </select>
                    </td>
                    <th class="formTitle"><span class="required">*</span>记账日期：</th>
                    <td class="formValue">
                        <input id="tdrq" type="text" class="form-control input-wdatepicker formClearIgnore" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" value="@(System.DateTime.Now.ToString("yyyy-MM-dd"))" />
                    </td>
                    <th class="formTitle">备注：</th>
                    <td class="formValue">
                        <input id="bz" type="text" class="form-control form-an form-an-end" value="" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
</div>

@Html.Partial("_MiddleButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel()
{
    ShowKeyList = new int[] { 2, 3, 6, 9 },
    F2Text = "添加",
    F3Text = "修改",
    F6Text = "删除",
    F9Text = "取消修改",
    F9Hidden = true
})

<div>
    <table id="gridMX"></table>
</div>


@Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel()
{
    ShowKeyList = new int[] { 4, 7, 8 },
    F7Text = "模板",
    F8Text = "提交"
})

<script type="text/javascript">

    //加载康复类别
    $("#kflb").bindSelect({
        url: "/Com/GetSelectItemsDetailListByItemCode?code=RehabTreatmentMethod",
    });
    $("#kflb").val('RTM_PT').trigger('change');

    $('#btn_bottombutton_f3').attr('disabled', 'disabled');
    $('#btn_bottombutton_f6').attr('disabled', 'disabled');

    //声明 项目明细 grid
    $("#gridMX").newtouchLocalDataGrid({
        height: $(window).height() - 350,
        unwritten: false,
        colModel: [
            { label: "newid", name: "newid", width: 80, align: "left", hidden: true },
            { label: "outerId", name: "outerId", width: 80, align: "left", hidden: true },
            {
                label: "记账日期", name: "tdrq", width: 80, align: "left",
                formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' }
            },
            { label: "收费项目", name: "sfxmCode", width: 150, align: "left", hidden: true },
            { label: "收费项目", name: "sfxmmc", width: 200, align: "left" },
            { label: "医嘱类型", name: "yzlx", width: 150, align: "left", hidden: true },
            { label: "类别", name: "sfdlCode", width: 50, align: "left", hidden: true },
            { label: "类别", name: "sfdlmc", width: 50, align: "left" },
            { label: "单位", name: "dw", width: 40, align: "left" },
            { label: "单位时长", name: "duration", width: 60, align: "left" },
            {
                label: "单价(元)", name: "dj", width: 60, align: "left", formatter: "number", hidden: true
                    , formatoptions: { decimalPlaces: 4, defaultValue: '0.0000' }
            },
            { label: "数量", name: "sl", width: 40, align: "left" },
            {
                label: "治疗师", name: "ysList", width: 180, align: "left", formatter: function (cellvalue) {
                    return getYsNamesByList(cellvalue);
                }
            },
            {
                label: "确认状态", name: "qrzt", width: 65, align: "left", formatter: function (cellvalue) {
                    return cellvalue === true ? "已确认"
                        : "未确认";
                }
            },
            { label: "备注", name: "bz", width: 160, align: "left" },
            { label: "团体标志", name: "ttbz", width: 50, align: "left", hidden: true },
            { label: "康复类别", name: "kflb", width: 50, align: "left", hidden: true },
        ],
        ondblClickRow: function (rowid) {
            gridEditRow(rowid);
        },
        onSelectRow: function (rowid) {
            $('#btn_bottombutton_f3').removeAttr('disabled');
            $('#btn_bottombutton_f6').removeAttr('disabled');
        },
        gridComplete: function (data) {
            var ids = $("#gridMX").jqGrid('getDataIDs');
            for (var i = 0; i < ids.length; i++) {
                var rowData = $("#gridMX").jqGrid('getRowData', ids[i]);
                if (rowData && rowData.qrzt) {
                    if (rowData.qrzt === '已确认') {
                        $('#gridMX tr[id="' + ids[i] + '"]').addClass('complete');
                    }
                }
            }
        }
    });

    //绑定 治疗师选择 树
    $('#zlsList').focus(function () {
        var initIdSelected = "";
        $.each(Editing_ysList, function () {
            initIdSelected += this.id + ",";
        })
        $.modalOpen({
            id: "StaffCorrelation",
            title: "选择治疗师",
            url: "/SystemManage/SysStaff/Selector?isContansChildOrg=false&callbackType=json&dutyCode=RehabDoctor&initIdSelected=" + initIdSelected,
            showleftlalbel: false,
            width: "650px",
            height: "570px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick(function (data) {
                    staffTreeCallback(data);
                });
            }
        });
    });

    //治疗师树 选择 回调
    function staffTreeCallback(treeSeleData) {
        var zlsNames = "";
        Editing_ysList = [];
        $.each(treeSeleData, function () {
            zlsNames += this.text + ",";
            Editing_ysList.push({ id: this.id, gh: this.value, Name: this.text, ks: this.Ex1, ksmc: this.Ex2 });
        })
        if (zlsNames.length > 0) {
            zlsNames = zlsNames.substring(0, zlsNames.length - 1);
            //给下一个 焦点
            //$(this).trigger('keydown');
            $('#kflb').trigger('focus');
        }
        $('#zlsList').val(zlsNames);
    }

    $('#zyh').keyupEnterEvent(function () {
        $('#btn_search').trigger('click');
    });

    $('#bz').keyupEnterEvent(function (event) {
        if ($('#bz').hasClass('form-an-cur')) { //缺陷-由上一个enter过来的
            $('#bz').removeClass('form-an-cur')
            return;
        }
        $('#btn_bottombutton_f2').trigger('click');
    });

    //治疗项目选择浮层绑定
    $('#sfxmmc').newtouchBatchFloatingSelector({
        width: 800,
        height: 280,
        caption: "选择项目",
        clickautotrigger: true,
        url: '/SystemManage/BaseData/SelectSfxmYp',
        ajaxparameters: function ($thisinput) {
            return "dllb=1,2,3&mzzybz=2&containyp0ck=1&keyword=" + $.trim($thisinput.val());
        },
        itemdbclickhandler: function ($thistr, $thisinput) {
            if ($thisinput.attr('id') == 'sfxmmc') {
                $('#sfxmmc').val($thistr.attr('data-sfxmmc'));
                $('#sfxmCode').val($thistr.attr('data-sfxmCode'));
                $('#sfdlCode').val($thistr.attr('data-sfdlCode'));
                $('#yzlx').val($thistr.attr('data-yzlx'));
                $('#sfdlmc').val($thistr.attr('data-sfdlmc'));
                '@(zyjzDjEditable.ToString().ToLower())' === 'true'
                    ? $('#dj').val($thistr.attr('data-dj'))
                    : $('#dj').html($thistr.attr('data-dj'));
                $('#dw').html($thistr.attr('data-dw'));
                if ($thistr.attr('data-duration') != "null") {
                    $('#duration').val($thistr.attr('data-duration'));
                }
                $('#sl').val('1');
            }
        },
        colModel: [
            { label: '代码', name: 'sfxmCode', widthratio: 8 },
            { label: '名称', name: 'sfxmmc', widthratio: 12 },
            { label: '医嘱类型', name: 'yzlx', widthratio: 8, hidden: true },
            { label: '拼音', name: 'py', widthratio: 8 },
            { label: '收费大类', name: 'sfdlCode', widthratio: 8, hidden: true },
            { label: '收费大类', name: 'sfdlmc', widthratio: 8 },
            { label: '单位', name: 'dw', widthratio: 6 },
            {
                label: '单价', name: 'dj', widthratio: 12, formatter: function (cellvalue) {
                    return !!cellvalue ? parseFloat(cellvalue).toFixed(4) : '';
                }
            },
            {
                label: '时长', name: 'duration', widthratio: 6, formatter: function (cellvalue) {
                    return !!cellvalue ? cellvalue : '';
                }
            },
            { label: '备注', name: 'bz', widthratio: 36 }
        ]
    });

    //按创建时间 倒叙 排列， 最新创建的 在最前
    var oldxmList = [];   //历史已提交项目List
    var newxmList = [];   //新项目List

    //向gridMX grid里填充数据
    function fillDataToGrid() {
        $("#gridMX").resetSelection();
        $("#gridMX").clearGridData();
        $("#gridMX").newtouchLocalDataGrid({ posttofirst: false }, oldxmList);
        for (var i = 0; i <= newxmList.length; i++) {
            $("#gridMX").newtouchLocalDataGrid(null, newxmList[newxmList.length - i]);
        }
    }

    fillDataToGrid();

    //解析医生数组，获取医生名称串
    function getYsNamesByList(ysList) {
        if (ysList) {
            //解析治疗师[{},{}]获取字符串
            var str = "";
            $.each(ysList, function () {
                str += this.Name + ',';
            });
            if (str.length > 0) {
                str = str.substring(0, str.length - 1);
            }
            return str;
        }
        return "";
    }

    //检查正在编辑的数据（待提交）的完整性
    function checkEditingRowData(rowData) {
        if (!rowData.ysList || rowData.ysList.length == 0) {
            $.modalAlert("请选择治疗师", 'warning');
        }
        if (!rowData.tdrq) {
            $.modalAlert("请选择记账日期", 'warning');
        }
        else if (!rowData.sfxmCode || !rowData.sfxmmc || !rowData.yzlx || !rowData.sfdlCode || !rowData.sfdlmc) {
            $.modalAlert("请选择收费项目", 'warning');
        }
        else if (rowData.dw == "") {
            $.modalAlert("缺少项目单位信息", 'warning');
        }
        else if (rowData.yzlx == '2' && (rowData.duration == "" || isNaN(rowData.duration))) {
            //治疗项目 才需要 时长
            $.modalAlert("请填写项目单位时长", 'warning');
        }
        else if (rowData.dj == "" || isNaN(rowData.dj)) {
            $.modalAlert("缺少项目单价信息", 'warning');
        }
        else if (rowData.sl == "" || isNaN(rowData.sl)) {
            $.modalAlert("请填写数量", 'warning');
        }
        else if (rowData.kflb == "") {
            $.modalAlert("请选择康复类别", 'warning');
        }
        else {
            return true;
        }
        return false;
    }

    //按钮 事件 start

    //键入住院号 搜索
    $('#btn_search').click(function () {
        var zyh = $('#zyh').val();
        newtouch_globalevent_f4();
        $('#zyh').val(zyh);
        ajaxLoadDataResult();
    });

    var respZyh;

    function ajaxLoadDataResult() {
        var zyh = $('#zyh').val();
        if (zyh) {
            $.najax({
                type: "GET",
                url: "/HospitalizationManage/BookkeepInHos/GetInpatientAccountingStatusDetail?zyh=" + zyh,
                loading: true,
                success: function (ajaxresp) {

                    respZyh = ajaxresp.data.patInfo.zyh;

                    $('#xm').html(ajaxresp.data.patInfo.xm);
                    $('#xb').html($.getGender(ajaxresp.data.patInfo.xb));
                    $("#nlshow").html(getAgeFromBirthTime({ begin: ajaxresp.data.patInfo.csny }).text);
                    $('#ryzd').html(ajaxresp.data.patInfo.ryzdmc);
                    $('#zjh').html(ajaxresp.data.patInfo.zjh);
                    $('#phone').html(ajaxresp.data.patInfo.phone);
                    $('#csny').html(ajaxresp.data.patInfo.csny ? ajaxresp.data.patInfo.csny.substring(0, 10) : '');
                    $('#ryrq').html(ajaxresp.data.patInfo.ryrq ? ajaxresp.data.patInfo.ryrq.substring(0, 10) : '');

                    //oldxmList = ajaxresp.data.jzjhList;

                    fillDataToGrid();

                    $('#sfxmmc').trigger('focus');

                    if (true) {     //开关
                        setTimeout('loadSyncedTreatmentRecord(' + ajaxresp.data.patInfo.zyh + ');', 50);
                    }
                },
                errorCallback: function (err) {
                    newtouch_globalevent_f4();
                    $('#zyh').trigger('focus');
                },
                complete: function () {
                }
            });
        }
        else {
            GetPatSerarchView();
        }
    }

    //选择的病人 callback
    function getPatInfoAjax(selePatInfo) {
        $('#zyh').val(selePatInfo.zyh);
        ajaxLoadDataResult();
    }

    //住院病人查询
    function GetPatSerarchView(zyh) {
        if (!!!zyh) {
            zyh = '';
        }
        $.modalOpen({
            id: "patSearch",
            title: "患者查询",
            url: "/PatientManage/AccountManage/PatSearchView?t=" + Math.random() + "&zyh=" + zyh,
            width: "700px",
            height: "600px",
            callBack: function (iframeId) {
                top.frames[iframeId].PatDbGrid(); //在弹出窗口事件
                //调用查询卡号和住院号
            }//窗口点确定的回调函数
        });
    }

    //默认为当前
    var default_Editing_ysList = [{ id: '@(ViewBag.CurYsStaffId)', gh: '@(ViewBag.CurYs)', Name: '@(ViewBag.CurYsmc)', ks: '@(ViewBag.CurKs)', ksmc: '@(ViewBag.CurKsmc)' }]; //编辑域中的治疗师
    var Editing_ysList = []; //编辑域中的治疗师

    function setDefaultYsList() {
        if (default_Editing_ysList && default_Editing_ysList.length > 0 && default_Editing_ysList[0].id) {
            Editing_ysList = default_Editing_ysList;
            $('#zlsList').val(Editing_ysList[0].Name);
        }
    }

    setDefaultYsList();

    //获取编辑域的对象数据
    function getEditRowData() {
        var data = {
            tdrq: $('#tdrq').val(),
            sfxmCode: $('#sfxmCode').val(),
            sfxmmc: $('#sfxmmc').val(),
            yzlx: $('#yzlx').val(),
            duration: $('#duration').val(),
            dj: '@(zyjzDjEditable.ToString().ToLower())' === 'true'
                ? $('#dj').val()
                : $('#dj').html(),
            dw: $('#dw').html(),
            sl: $('#sl').val(),
            sfdlCode: $('#sfdlCode').val(),
            sfdlmc: $('#sfdlmc').val(),
            bz: $('#bz').val(),
            ttbz: $('#ttbz').is(':checked') ? "1" : "0",
            kflb: $('#kflb').val(),
        };
        //治疗师
        data.ysList = Editing_ysList;

        return data;
    }

    var editingNewid = null;    //正在修改的行
    var editingOuterId = null;    //正在修改的行

    //请求 编辑 行
    function gridEditRow(rowid) {
        if (rowid) {
            var rowData = $("#gridMX").jqGrid('getRowData', rowid);
            if (rowData) {
                if (rowData.newid) {
                    //应该是属于newxmList start
                    rowDataArr = $.jsonWhere(newxmList, function (v) {
                        return v && v.newid == rowData.newid;
                    });
                    if (!rowDataArr || rowDataArr.length == 0) {
                        return; //为什么没找到
                    }
                    rowData = rowDataArr[0];  //通过rowData.newid从对象数组中取出该行数据
                    if (true) { //&&可编辑
                        editingNewid = rowData.newid;    //正在修改的行
                        editingOuterId = rowData.outerId;   //正在修改的行
                    }
                    else {
                        return;
                    }
                    //应该是属于newxmList end
                }
                else {
                    //不可以编辑

                }
                //序列化至编辑域中
                $('#tdrq').val($.getDate({ date: new Date(rowData.tdrq) }));
                //治疗师
                Editing_ysList = rowData.ysList;
                $('#zlsList').val(getYsNamesByList(rowData.ysList)); //治疗师
                $('#sfxmmc').val(rowData.sfxmmc);
                $('#sfxmCode').val(rowData.sfxmCode);
                $('#yzlx').val(rowData.yzlx);
                $('#sfdlCode').val(rowData.sfdlCode);
                $('#sfdlmc').val(rowData.sfdlmc);
                $('#dw').html(rowData.dw);
                '@(zyjzDjEditable.ToString().ToLower())' === 'true'
                    ? $('#dj').val(rowData.dj)
                    : $('#dj').html(rowData.dj);
                $('#duration').val(rowData.duration);
                $('#sl').val(rowData.sl);
                $('#bz').val(rowData.bz);
                rowData.ttbz == "1" ? $('#ttbz').prop("checked", true) : $('#ttbz').removeAttr('checked');
                $('#kflb').val(rowData.kflb).trigger('change');

                $('#btn_bottombutton_f6').attr('disabled', 'disabled');
                $('#btn_bottombutton_f9').show();

                $('#btn_bottombutton_f2').attr('disabled', 'disabled');
            }
        }
    }

    //添加按钮
    function newtouch_event_f2() {
        if (editingNewid) {
            return; //正在处于修改状态，是不能点击添加按钮的   要disable处理
        }

        var data = getEditRowData();
        if (!(checkEditingRowData(data))) {    //数据是否完善，检查必填项
            return;
        }

        //检查重复项

        data.newid = Math.random().toString() + new Date().getMilliseconds();   //newid()

        newxmList.unshift(data);   //作为新项添加 //”数组最前端“

        //重新将xm数组呈现至grid
        fillDataToGrid();

        $('#sfxmmc').val('');
        $('#sfxmCode').val('');
        $('#yzlx').val('');
        $('#sfdlCode').val('');
        $('#sfdlmc').val('');
        $('#dw').html('');
        '@(zyjzDjEditable.ToString().ToLower())' === 'true'
            ? $('#dj').val('')
            : $('#dj').html('');
        $('#duration').val('');
        $('#sl').val('');
        $('#ttbz').removeAttr('checked');
        //$('#kflb').val('').trigger('change');

        $('#sfxmmc').trigger('focus');
    }

    //修改按钮
    function newtouch_event_f3() {
        if (editingNewid) {
            //提交修改
            var data = getEditRowData();
            if (!(checkEditingRowData(data))) {    //数据是否完善，检查必填项
                return;
            }
            if (data.yzxz == '1') {
                data.EndDate = data.StartDate;
            }

            data.newid = editingNewid;
            data.outerId = editingOuterId;

            //检查重复项

            //先在xm数组中找到之
            var matchedIndex = -1;
            for (var ii = 0; ii < newxmList.length; ii++) {
                if (newxmList[ii].newid == data.newid) {
                    matchedIndex = ii;
                    break;
                }
            }
            if (matchedIndex == -1) {
                return; //应该是异常
            }
            newxmList.remove(matchedIndex);   //移除该项
            newxmList.unshift(data);   //作为新项添加    //”数组最前端“

            //重新将xm数组呈现至grid
            fillDataToGrid();

            $('#sfxmmc').val('');
            $('#sfxmCode').val('');
            $('#yzlx').val('');
            $('#sfdlCode').val('');
            $('#sfdlmc').val('');
            $('#dw').html('');
            '@(zyjzDjEditable.ToString().ToLower())' === 'true'
                ? $('#dj').val('')
                : $('#dj').html('');
            $('#duration').val('');
            $('#ttbz').removeAttr('checked');
            //$('#kflb').val('').trigger('change');
            $('#sl').val('');

            editingNewid = null;    //正在修改的行
            editingOuterId = null;    //正在修改的行

            bottomButtonsReset();

            $('#sfxmmc').trigger('focus');
        }
        else {
            //获取grid当前选中的那一行
            var seleId = $('#gridMX').jqGrid('getGridParam', 'selrow');
            if (seleId) {
                //请求修改该行
                gridEditRow(seleId);
            }
        }
    }

    //F4清除
    function newtouch_event_f4() {
        $("#gridMX").clearGridData();

        oldxmList = [];   //历史已提交项目List
        newxmList = [];   //新项目List
        Editing_ysList = [];
        editingNewid = null;    //正在修改的行
        editingOuterId = null;  //正在修改的行

        bottomButtonsReset();

        setDefaultYsList();
    };

    //F7使用模板
    function newtouch_event_f7() {
        var ksList = "";
        $.each(Editing_ysList, function (idx, val) {
            ksList += val.ks + ",";
        });
        if (ksList) {
            ksList = ksList.substring(0, ksList.length - 1);
            $.modalOpen({
                id: "Form",
                title: "收费项目模板窗口",
                url: "/HospitalizationManage/BookkeepInHos/ChargeItemTemplate?keyValue=" + ksList,
                width: "700px",
                height: "530px",
                callBack: function (iframeId) {
                    var data = top.frames[iframeId].submitForm();
                    $.each(data, function () {
                        var newRowData = {
                            tdrq: $.getDate(),
                            sfxmCode: this.sfxm,
                            sfxmmc: this.sfxmmc,               //    ？？？？
                            yzlx: "2",  //项目
                            duration: this.duration,
                            dj: this.dj,
                            dw: this.dw,
                            sl: this.sl,
                            sfdlCode: this.dl,
                            sfdlmc: this.dlmc,
                            bz: '',
                            ttbz: "0",
                            kflb: '',
                        };
                        newRowData.ysList = Editing_ysList;

                        newRowData.newid = Math.random().toString() + new Date().getMilliseconds();   //newid()
                        if (newRowData.sfxmCode && newRowData.sfdlCode && newRowData.dj && newRowData.dw && newRowData.sl) {
                            newRowData.dj = parseFloat(newRowData.dj);
                            newRowData.sl = parseInt(newRowData.sl);
                            newxmList.unshift(newRowData);   //作为新项添加 //”数组最前端“
                        }
                    });

                    top.frames[iframeId].$.modalClose();

                    fillDataToGrid();
                }
            });
        }
        else {
            $.modalAlert("请选择治疗师！", 'error');
        }
    }

    //F8提交至服务器
    function newtouch_event_f8() {
        var paramData = {};
        paramData.zyh = $('#zyh').val();

        paramData.xmList = newxmList;
        if (!paramData.zyh) {
            $.modalAlert("缺少住院号", 'warning');
            return;
        }
        if (paramData.zyh != respZyh) {
            $.modalAlert("错误：录入过程中，住院号变换", 'warning');
            return;
        }
        if (!paramData.xmList || paramData.xmList.length == 0) {
            $.modalAlert("缺少记账项目", 'warning');
            return;
        }

        for (var k = 0, length = paramData.xmList.length; k < length; k++) {
            if (!checkEditingRowData(paramData.xmList[k])) {
                return;
            }
        }

        $.submitForm({
            url: '/HospitalizationManage/BookkeepInHos/InpatientAccountingSubmitAccounting',
            param: paramData,
            success: function (data) {

                newtouch_globalevent_f4();

                $('#zyh').trigger('focus');
            }
        });
    };


    //F6从列表中删除
    function newtouch_event_f6() {
        //获取grid当前选中的那一行
        var rowid = $('#gridMX').jqGrid('getGridParam', 'selrow');
        if (rowid) {
            //请求删除该行
            if (rowid) {
                var rowData = $("#gridMX").jqGrid('getRowData', rowid);
                if (rowData) {
                    if (!rowData.newid) {   //该行不能删除
                        $.modalAlert("已确认的记账，不能删除", 'warning');
                        return;
                    }
                    if (rowData.newid == editingNewid) {
                        $.modalAlert("该行正在处于编辑状态，不能删除", 'warning');
                        return; //该行正在处于编辑状态，不能删除
                    }

                    var matchedIndex = -1;
                    for (var ii = 0; ii < newxmList.length; ii++) {
                        if (newxmList[ii].newid == rowData.newid) {
                            matchedIndex = ii;
                            break;
                        }
                    }

                    if (matchedIndex == -1) {
                        return; //应该是异常
                    }

                    newxmList.remove(matchedIndex);   //移除该项

                    //重新将xm数组呈现至grid
                    fillDataToGrid();

                    //对其他都不产生影响
                }
            }
        }
    }

    //F9取消修改
    function newtouch_event_f9() {
        $('#tdrq').val($.getDate());
        $('#sfxmmc').val('');
        $('#sfxmCode').val('');
        $('#yzlx').val('');
        $('#sfdlCode').val('');
        $('#sfdlmc').val('');
        $('#dw').html('');
        '@(zyjzDjEditable.ToString().ToLower())' === 'true'
          ? $('#dj').val('')
          : $('#dj').html('');
        $('#duration').val('');
        $('#ttbz').removeAttr('checked');
        //$('#kflb').val('').trigger('change');
        $('#sl').val('');
        //$('#bz').val('');

        editingNewid = null;    //正在修改的行
        editingOuterId = null;    //正在修改的行

        $("#gridMX").resetSelection();

        bottomButtonsReset();

        $('#sfxmmc').trigger('focus');
    }

    //
    function bottomButtonsReset() {
        $('#btn_bottombutton_f2').removeAttr('disabled');
        $('#btn_bottombutton_f3').attr('disabled', 'disabled');
        $('#btn_bottombutton_f6').attr('disabled', 'disabled');
        $('#btn_bottombutton_f9').hide();
    }

    //按钮 事件 end

    //加载同步治疗记录
    function loadSyncedTreatmentRecord(zyh) {
        if (zyh) {
            $.najax({
                type: "GET",
                url: "/HospitalizationManage/BookkeepInHos/GetUnConfirmedSyncedTreatmentRecord?zyh=" + zyh,
                loading: true,
                loadingtext: "正在请求同步治疗记录",
                success: function (ajaxresp) {
                    if (ajaxresp && ajaxresp.data && ajaxresp.data && ajaxresp.data.length > 0) {
                        $.each(ajaxresp.data, function () {
                            var newRowData = {
                                tdrq: this.CreatedDate,
                                sfxmCode: this.serviceCode,
                                sfxmmc: this.serviceDescription,
                                yzlx: "2",
                                duration: this.duration,
                                dj: this.dj,
                                dw: this.dw,
                                sl: this.sl,
                                sfdlCode: this.sfdlCode,
                                sfdlmc: this.sfdlmc,
                                bz: '',
                                ttbz: "0",
                                kflb: this.kflb,
                                ysList: [{ id: this.StaffId, gh: this.therapistId, Name: this.therapistName, ks: this.ks, ksmc: this.ksmc }],
                                outerId: this.outerId
                            };

                            newRowData.newid = Math.random().toString() + new Date().getMilliseconds();   //newid()
                            newxmList.unshift(newRowData);   //作为新项添加 //”数组最前端“
                        });

                        fillDataToGrid();

                        //$.modalMsg("成功请求到 <b>" + ajaxresp.data.length + '</b> 条未确认记录', 'ok', 1200);
                    }
                }
            });
        }
    }


</script>
